<template>
  <div>
    <ElCard>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="一级分类">
          <el-select v-model="AttrStore.c1ID" @change="handlerC1ID" :disabled="scene > 0">
            <el-option
              v-for="c1 in AttrStore.c1Arr"
              :key="c1.id"
              :label="c1.name"
              :value="c1.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select v-model="AttrStore.c2ID" @change="handlerC2ID" :disabled="scene > 0">
            <el-option v-for="c2 in AttrStore.c2Arr" :key="c2.id" :label="c2.name" :value="c2.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="三级分类">
          <el-select v-model="AttrStore.c3ID" @change="handlerC3ID" :disabled="scene > 0">
            <el-option v-for="c3 in AttrStore.c3Arr" :key="c3.id" :label="c3.name" :value="c3.id" />
          </el-select>
        </el-form-item>
      </el-form>
    </ElCard>
  </div>
</template>

<script lang="ts" setup name="Category">
import { reqC1, reqC2, reqC3 } from '@/API/product/attr'
import { CategoryResponseData } from '@/API/product/attr/type'
import { onMounted, ref } from 'vue'
import useAttrStore from '@/store/modules/useAttr'
import { onBeforeUnmount } from 'vue'

let AttrStore = useAttrStore()

// 一级菜单的某个选项的值被我们选中后获取二级菜单的内容,情况2级菜单的id以及3级的内容,防止bug.
const handlerC1ID = () => {
  AttrStore.c2ID = ''
  AttrStore.c3Arr = []
  AttrStore.c3ID = ''
  getC2()
}
/* 二级的值被选中后,清空三级菜单的id再获取数据.防止bug */
const handlerC2ID = () => {
  AttrStore.c3ID = ''
  getC3()
}
const handlerC3ID = () => {
  console.log(AttrStore.c3ID)
}

// 获取一集数据
const getC1 = async () => {
  await reqC1().then((result) => {
    if (result.code == 200) {
      AttrStore.c1Arr = result.data
    }
  })
}
// 获取二集数据
const getC2 = async () => {
  let result: CategoryResponseData = await reqC2(AttrStore.c1ID)

  if (result.code == 200) {
    AttrStore.c2Arr = result.data
  } else new Error('获取数据失败')
}
// 获取三集数据.value
const getC3 = async () => {
  let result: CategoryResponseData = await reqC3(AttrStore.c2ID)

  if (result.code == 200) {
    AttrStore.c3Arr = result.data
  } else new Error('获取数据失败')
}
/* 组件挂载完毕 */
onMounted(() => {
  /* 获取一集数据 */
  getC1()
})
// 接受父组件传过来的scene
defineProps(['scene'])
onBeforeUnmount(() => {
  AttrStore.reset()
})
</script>

<style scoped>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
